package org.itboys.core.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2018/10/17
 * 描述：江南布衣请求
 */
public class JNBYRequestUtil {
    /**
     * 江南布衣请求
     * @param paramJson
     * @param token
     * @param url
     * @return
     * @throws IOException
     */
    public static JSONObject request(JSONObject paramJson,String token,String url) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        //post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
        parameters.add(new BasicNameValuePair("token", token));
        parameters.add(new BasicNameValuePair("paramJson", paramJson.toJSONString()));
        post.setEntity(new UrlEncodedFormEntity(parameters,"UTF-8"));
        HttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity, "UTF-8");


        JSONObject json= (JSONObject) JSONObject.parse(result);
        return json;
    }
}
